<?php

declare(strict_types=1);

namespace App\Model\Act;

use App\Model\CoinLog;
use App\Model\UserCoin;
use App\Model\UserProfile;
use Hyperf\DbConnection\Model\Model;

/**
 *  完善资料奖励
 */
class ProfileRewardLog extends Model
{
    protected $table = 'act_profile_reward_log';
    protected $fillable = ['user_id', 'reward_coin', 'reward_type'];

    /**
     * 奖励
     */
    static function doReward($userId = 0, $rewardType = 'voice', $rewardCoin = 2)
    {
        //头像外其他资料完善不奖励
        if (!$userId) return false;
        $userGender = UserProfile::getGender($userId);
        //非男性不奖励
        if ($userGender != 1) return false;
        $where = [
            'user_id' => $userId,
            'reward_type' => $rewardType
        ];
        $check = self::where($where)->first();
        if ($check) return false;
        //添加记录
        self::updateOrCreate($where, [
            'user_id' => $userId,
            'reward_type' => $rewardType,
            'reward_coin' => $rewardCoin,
        ]);
        //执行赠送
        $remainCoin = UserCoin::gainIncome($userId, $rewardCoin);
        //添加流水记录
        CoinLog::addIncome($userId, $rewardCoin, CoinLog::FROM_PROFILE_FIX, $remainCoin);
        return true;
    }

    /**
     * 资料是否全部完成
     */
    static function checkAllComplete($userId = 0, $profile = null)
    {
        if (!$profile) return false;
        $fields = ['voice', 'self_intro', 'poster', 'city'];
        foreach ($fields as $field) if (!$profile->{$field}) return false;
        return true;
        // return self::where(['user_id' => $userId])->count() >= 6;
    }
}
